package com.jijuxie.house.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jijuxie.house.domain.HouseAppointment;
import com.jijuxie.house.domain.HouseInfo;
import com.jijuxie.house.mapper.HouseAppointmentMapper;
import com.jijuxie.house.service.IHouseAppointmentService;
import com.jijuxie.house.service.IHouseService;
import com.jijuxie.common.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class HouseAppointmentServiceImpl extends ServiceImpl<HouseAppointmentMapper, HouseAppointment> implements IHouseAppointmentService {

    @Autowired
    private IHouseService houseService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void makeAppointment(HouseAppointment appointment) {
        // 检查房源是否存在
        HouseInfo houseInfo = houseService.getById(appointment.getHouseId());
        if (houseInfo == null) {
            throw new ServiceException("房源不存在");
        }

        // 检查预约时间是否合法
        if (appointment.getAppointmentTime().isBefore(LocalDateTime.now())) {
            throw new ServiceException("预约时间不能早于当前时间");
        }

        // 检查是否重复预约
        long count = lambdaQuery()
                .eq(HouseAppointment::getHouseId, appointment.getHouseId())
                .eq(HouseAppointment::getUserId, appointment.getUserId())
                .eq(HouseAppointment::getAppointmentTime, appointment.getAppointmentTime())
                .count();
        if (count > 0) {
            throw new ServiceException("该时间段已被预约");
        }

        // 设置默认状态
        appointment.setStatus("0");
        save(appointment);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void cancelAppointment(Long appointmentId) {
        HouseAppointment appointment = getById(appointmentId);
        if (appointment == null) {
            throw new ServiceException("预约不存在");
        }

        // 只能取消待确认的预约
        if (!"0".equals(appointment.getStatus())) {
            throw new ServiceException("当前状态不能取消预约");
        }

        // 更新预约状态为已取消
        appointment.setStatus("2");
        updateById(appointment);
    }

    @Override
    public Page<HouseAppointment> selectAppointmentPage(Page<HouseAppointment> page, Long userId) {
        return lambdaQuery()
                .eq(HouseAppointment::getUserId, userId)
                .orderByDesc(HouseAppointment::getCreateTime)
                .page(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void confirmAppointment(Long appointmentId) {
        HouseAppointment appointment = getById(appointmentId);
        if (appointment == null) {
            throw new ServiceException("预约不存在");
        }

        // 只能确认待确认的预约
        if (!"0".equals(appointment.getStatus())) {
            throw new ServiceException("当前状态不能确认预约");
        }

        // 更新预约状态为已确认
        appointment.setStatus("1");
        updateById(appointment);
    }

    @Override
    public Page<HouseAppointment> selectAgentAppointmentPage(Page<HouseAppointment> page, Long agentId) {
        return lambdaQuery()
                .eq(HouseAppointment::getAgentId, agentId)
                .orderByDesc(HouseAppointment::getCreateTime)
                .page(page);
    }

    @Override
    public void addAppointment(HouseAppointment appointment) {
        makeAppointment(appointment);  // 复用已有的 makeAppointment 方法
    }
}
